2

2 つの xml ファイルがあります。1 つは実際のデータで、もう 1 つはデータから抽出したいノードのキーのリストです。多くの検索とテストを行った後、助けを求めたいと思いました。望む結果が得られません。

データファイル:

<?xml version="1.0" encoding="UTF-8"?>
<Items>
  <Item>
    <ItemKey>12345</ItemKey>
    <Name>Apple></Name>
    <Attribute>Red</Attribute>
  </Item>
  <Item>
    <ItemKey>67890</ItemKey>
    <Name>Orange</Name>
    <Attribute>Orange</Attribute>
  </Item>
  <Item>
    <ItemKey>12346</ItemKey>
    <Name>Grape</Name>
    <Attribute>Purple</Attribute>
  </Item>
  <Item>
    <ItemKey>67891</ItemKey>
    <Name>Pear</Name>
    <Attribute>Yellow</Attribute>
  </Item>
</Items>

フィルタ ファイル:

<?xml version="1.0" encoding="UTF-8"?>
<Items>
  <Item>
    <ItemKey>12345</ItemKey>
    </Item>
  <Item>
    <ItemKey>12346</ItemKey>
  </Item>
</Items>

データ ファイルを変換して、フィルタ ファイルの ItemKey に一致するサブノードとサブノードのみを抽出しようとしています。私が見たいくつかの例を使用してみましたが、次の xml を使用しても成功しませんでした。

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>

    <xsl:variable name="filter" select="document('Filter.xml')/Item/*"/>

    <xsl:template match="/">
        <xsl:for-each select="Items/Item">
            <xsl:choose>
                <xsl:when test="$filter/Item/ItemKey[contains(., ./ItemKey)]">
                    <xsl:call-template name="Filtered"/>
                </xsl:when>
            </xsl:choose>
        </xsl:for-each>
    </xsl:template>

    <xsl:template name="Filtered">
        <xsl:text>&#x9;"</xsl:text>
        <xsl:value-of select="ItemKey" />, <xsl:value-of select="Name"/>
        <xsl:text>",&#xA;</xsl:text>
    </xsl:template>
</xsl:stylesheet>

ご覧のとおり、テキストに変換したいので、必要なノードを取得していることがわかったら、適切にフォーマットします。ご協力いただきありがとうございます。

4

1 に答える 1

0

これと同じくらい簡単です:

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

 <xsl:template match="/">
     <xsl:copy-of select=
     "/*/Item[ItemKey = document('file:///c:/temp/delete/filter.xml')/*/*/ItemKey]"/>
 </xsl:template>
</xsl:stylesheet>

この変換が提供されたソース XML ドキュメントに適用されると、次のようになります。

<Items>
  <Item>
    <ItemKey>12345</ItemKey>
    <Name>Apple></Name>
    <Attribute>Red</Attribute>
  </Item>
  <Item>
    <ItemKey>67890</ItemKey>
    <Name>Orange</Name>
    <Attribute>Orange</Attribute>
  </Item>
  <Item>
    <ItemKey>12346</ItemKey>
    <Name>Grape</Name>
    <Attribute>Purple</Attribute>
  </Item>
  <Item>
    <ItemKey>67891</ItemKey>
    <Name>Pear</Name>
    <Attribute>Yellow</Attribute>
  </Item>
</Items>

提供された「フィルター xml」はファイルにあります: c:\temp\delete\filter.xml:

<Items>
  <Item>
    <ItemKey>12345</ItemKey>
    </Item>
  <Item>
    <ItemKey>12346</ItemKey>
  </Item>
</Items>

必要な正しい結果が生成されます。

<Item>
   <ItemKey>12345</ItemKey>
   <Name>Apple&gt;</Name>
   <Attribute>Red</Attribute>
</Item>
<Item>
   <ItemKey>12346</ItemKey>
   <Name>Grape</Name>
   <Attribute>Purple</Attribute>
</Item>

説明:

標準 XSLT 関数の適切な使用document()

于 2013-01-13T17:39:13.713 に答える