0

XSLT シートでの作業に少し問題があります。これは私の XML ドキュメントです:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <products>
    <product>
      <id>1</id>
    </product>
  </products>
  <stocks>
    <stock>
      <id>1</id>
      <size>S</size>
      <store>NYC</store>
    </stock>
    <stock>
      <id>1</id>
      <size>L</size>
      <store>NYC</store>
    </stock>
    <stock>
      <id>1</id>
      <size>S</size>
      <store>LA</store>
    </stock>
  </stocks>
</catalog>

私が欲しいのは、この種の出力 XML を持つことです:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <products>
    <product>
      <id>1</id>
      <variants>
        <variant>
          <size>S</size>
          <stocks>
            <stock store-ref="NYC">
            <stock store-ref="LA">
          </stocks>
        <variant>
        <variant>
          <size>L</size>
          <stocks>
            <stock store-ref="NYC">
          </stocks>
        <variant>
      </variants>
    </product>
  </products>
</catalog> 

今日、私はこの XSLT を使用してこの変換を実行しています。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" exclude-result-prefixes="xs fn" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
  <xsl:key name="sizes" match="stock" use="id"/>
  <xsl:key name="stocks" match="stock" use="fn:concat(id, '-', size)"/>
  <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
  <xsl:template match="/">
    <catalog>
      <products>
        <xsl:for-each select="/catalog/products/product">
          <product>
            <id><xsl:value-of select="id" /></id>
            <variants>
              <xsl:for-each select="key('sizes', id)">
                <variant>
                  <size><xsl:value-of select="size" /></size>
                  <stocks>
                    <xsl:for-each select="key('stocks', fn:concat(id, '-', size))">
                      <stock store-ref="{store}" />
                    </xsl:for-each>
                  </stocks>
                </variant>
              </xsl:for-each>
            </variants>
          </product>
        </xsl:for-each>
      </products>
    </catalog>
  </xsl:template>
</xsl:stylesheet>

そして、私はこの結果を得ました:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
   <products>
      <product>
         <id>1</id>
         <variants>
            <variant>
               <size>S</size>
               <stocks>
                  <stock store-ref="NYC"/>
                  <stock store-ref="LA"/>
               </stocks>
            </variant>
            <variant>
               <size>L</size>
               <stocks>
                  <stock store-ref="NYC"/>
               </stocks>
            </variant>
            <variant>
               <size>S</size>
               <stocks>
                  <stock store-ref="NYC"/>
                  <stock store-ref="LA"/>
               </stocks>
            </variant>
         </variants>
      </product>
   </products>
</catalog>

したがって、私の問題は、個別のサイズ値を選択したいのですが、うまくいかないようです。generate-id() を使用しようとしましたが、それがどのように機能するかをよく理解していないため、素晴らしい結果が得られませんでした:(これを修正する方法はありますか?ありがとう!

4

2 に答える 2

2

@Tomalak のおかげで、解決策が見つかりました。これが私の作業中の XSLT です:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" exclude-result-prefixes="xs fn" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
  <xsl:key name="sizes" match="stock" use="id"/>
  <xsl:key name="stocks" match="stock" use="fn:concat(id, '-', size)"/>
  <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
  <xsl:template match="/">
    <catalog>
      <products>
        <xsl:for-each select="/catalog/products/product">
          <product>
            <id><xsl:value-of select="id" /></id>
            <variants>
              <xsl:for-each-group select="key('sizes', id)" group-by="size">
                <variant>
                  <size><xsl:value-of select="size" /></size>
                  <stocks>
                    <xsl:for-each select="key('stocks', fn:concat(id, '-', size))">
                      <stock store-ref="{store}" />
                    </xsl:for-each>
                  </stocks>
                </variant>
              </xsl:for-each-group>
            </variants>
          </product>
        </xsl:for-each>
      </products>
    </catalog>
  </xsl:template>
</xsl:stylesheet>
于 2014-06-18T08:54:58.287 に答える