0

im trying to filter some book records from a larger metadata file. I only need records with a specific term in the field "rights". If the field contains "インターネット公開" it should go to the result every other record is unessary.

Sample xml:

<ListRecords>
  <record>
    <header>
    </header>
    <metadata>
      <title>農林時報</title>
      <creator>農林省</creator>
      <rights>国立国会図書館</rights>
    </metadata>
  </record>
  <record>
    <header>
    </header>
    <metadata>
      <title>大和本草</title>
      <creator>貝原篤信</creator>
      <rights>インターネット公開(保護期間満了)</rights>
    </metadata>
  </record>
</ListRecords>

My xsl:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns="http://www.w3.org/TR/REC-html40"> 

<xsl:output method="xml" encoding="utf-16" indent="no" />

<xsl:template match="@*|node()">
    <xsl:if test="contains(current(), 'インターネット公開')">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
        </xsl:if>
</xsl:template>
</xsl:stylesheet>

The Result should show the hole xml structure but only records with the specified term.

I would be grateful for any help.

Hanzo D.
  • 1
  • 1

1 Answers1

0

Your attempt will copy only elements that contain the prescribed string in their own text value. If you want to copy entire records, why don't you do simply:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/ListRecords">
    <xsl:copy>
        <xsl:copy-of select="record[contains(metadata/rights, 'インターネット公開')]"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>
michael.hor257k
  • 96,733
  • 5
  • 30
  • 46
  • Thank you for your answer, im testing your xslt with oxygen. In the result there are only values but no tags and it copys every record. – Hanzo D. Mar 24 '17 at 07:37
  • Maybe you can help me one more time because i think i cant get along with xslt. I uploaded the original xml: http://xsltransform.net/bFWR5Fe/3 I tryed my sample in Oxygen but i dont know why its not working. Thanks in advance. – Hanzo D. Mar 24 '17 at 09:54
  • @HanzoD. It's not working, because your input (a) has a different structure (`ListRecords` is not the root element) and (b) it has a default namespace that needs to be taken into account. – michael.hor257k Mar 25 '17 at 08:41
  • Ive tryed several other configurations of course. Added the namespaces and other structure but didnt work, see: http://xsltransform.net/bFWR5Fe/5 – Hanzo D. Mar 28 '17 at 09:50
  • See: http://stackoverflow.com/questions/34758492/xslt-transform-doesnt-work-until-i-remove-root-node/34762628#34762628 -- P.S. I believe this question is answered. If you have other issues that you're unable to solve on your own, please post a new question. – michael.hor257k Mar 28 '17 at 09:58