0

我需要为产品生成整数 ID,然后通过输出中的整数 ID 引用相关产品。在输入中,我有代表这种关系的字符串键。谢谢您的帮助。

输入:

<root>
  <products>
    <product>
      <!-- a unique string key of this node between the other product nodes -->
      <stringKey>AppleRef</stringKey>
      <Name>Apple</Name>
      <relatedProducts>
        <!-- a reference to product/StringKey of Orange -->
        <relatedProductStringKey>OrangeRef</relatedProductStringKey>
        <!-- other related products may follow -->
      </relatedProducts>
    </product>
    <product>
      <stringKey>OrangeRef</stringKey>
      <Name>Orange</Name>
      <relatedProducts>
        <relatedProductStringKey>AppleRef</relatedProductStringKey>
      </relatedProducts>
    </product>
  </products>
</root>

预期输出:

<root>
  <products>
    <P>
      <ProductInfo>
        <!-- a unique integer ID of this node between the other ProductsInfo nodes -->
        <ProductID>0</ProductID>
        <ProductRef>AppleRef</ProductRef>
        <ProductName>Apple</ProductName>
      </ProductInfo>
      <R>
        <ProductRelatedInfo>

          <!-- a unique integer ID of this node between the other ProductRelatedInfo nodes -->
          <RelatedID>0</RelatedID>

          <!-- a reference to ProductInfo/ProductID of Orange -->
          <RelatedProductID>1</RelatedProductID>

        </ProductRelatedInfo>
        <!-- other related products may follow -->

      </R>
    </P>
    <P>
      <ProductInfo>
        <ProductID>1</ProductID>
        <ProductRef>OrangeRef</ProductRef>
        <ProductName>Orange</ProductName>
      </ProductInfo>
      <R>
        <ProductRelatedInfo>
          <RelatedID>1</RelatedID>
          <RelatedProductID>0</RelatedProductID>
        </ProductRelatedInfo>
      </R>
    </P>
  </products>
</root>
4

2 回答 2

1

要在 XSLT 中获取整数 ID,您可以使用文档中节点的位置,您可以通过计算其前面的节点来获得。

以下样式表将产生您想要的结果:

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

    <xsl:output indent="yes"/>

    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>

    <!-- match product node -->
    <xsl:template match="product">
        <P>
            <ProductInfo>
                <ProductID>
                    <!-- count preceding-sibling products -->
                    <xsl:value-of select="count(preceding-sibling::product)"/>
                </ProductID>
                <ProductRef>
                    <xsl:value-of select="stringKey"/>
                </ProductRef>
                <ProductName>
                    <xsl:value-of select="Name"/>
                </ProductName>
            </ProductInfo>
            <xsl:apply-templates select="relatedProducts"/>
        </P>
    </xsl:template>

    <xsl:template match="relatedProducts">
        <R>
            <xsl:for-each select="relatedProductStringKey">
                <!-- apply on product that has a matching stringkey -->
                <xsl:apply-templates select="/root/products/product[stringKey=current()]" mode="related">
                    <!-- 
                        RelatedID will be unique within each relatedProducts node, 
                        if you want this to be unique across the document use preceding:: instead 
                    -->
                    <xsl:with-param name="RelatedID" select="count(preceding-sibling::relatedProductStringKey)"/>
                </xsl:apply-templates>
            </xsl:for-each>
        </R>
    </xsl:template>

    <!-- match product node for related products -->
    <xsl:template match="product" mode="related">
        <xsl:param name="RelatedID"/>
        <ProductRelatedInfo>

            <!-- a unique integer ID of this node between the other ProductRelatedInfo nodes -->
            <RelatedID>
                <xsl:value-of select="$RelatedID"/>
            </RelatedID>

            <!-- a reference to ProductInfo/ProductID of Orange -->
            <RelatedProductID>
                <!-- count preceding-sibling products again -->
                <xsl:value-of select="count(preceding-sibling::product)"/>
            </RelatedProductID>

        </ProductRelatedInfo>
    </xsl:template>

</xsl:stylesheet>
于 2014-09-12T13:37:26.920 回答
1

使用key很容易做到这一点。例如,以下样式表:

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:key name="product" match="product" use="stringKey" />

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="stringKey">
    <ProductID><xsl:value-of select="count(../preceding-sibling::product)"/></ProductID>
    <ProductRef><xsl:value-of select="."/></ProductRef>
</xsl:template>

<xsl:template match="relatedProductStringKey">
    <RelatedProductID><xsl:value-of select="count(key('product', .)/preceding-sibling::product)"/></RelatedProductID>
</xsl:template>

</xsl:stylesheet>

当应用于您的输入时,将返回:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <products>
      <product>
         <ProductID>0</ProductID>
         <ProductRef>AppleRef</ProductRef>
         <Name>Apple</Name>
         <relatedProducts>
            <RelatedProductID>1</RelatedProductID>
         </relatedProducts>
      </product>
      <product>
         <ProductID>1</ProductID>
         <ProductRef>OrangeRef</ProductRef>
         <Name>Orange</Name>
         <relatedProducts>
            <RelatedProductID>0</RelatedProductID>
         </relatedProducts>
      </product>
   </products>
</root>

如果您更喜欢无意义但不一定是数字 ID,您可能更喜欢更简单的:

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:key name="product" match="product" use="stringKey" />

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="stringKey">
    <ProductID><xsl:value-of select="generate-id(..)"/></ProductID>
    <ProductRef><xsl:value-of select="."/></ProductRef>
</xsl:template>

<xsl:template match="relatedProductStringKey">
    <RelatedProductID><xsl:value-of select="generate-id(key('product', .))"/></RelatedProductID>
</xsl:template>

</xsl:stylesheet>

确切的结果取决于处理器,例如 Saxon 可能会返回:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <products>
      <product>
         <ProductID>d0e3</ProductID>
         <ProductRef>AppleRef</ProductRef>
         <Name>Apple</Name>
         <relatedProducts>
            <RelatedProductID>d0e11</RelatedProductID>
         </relatedProducts>
      </product>
      <product>
         <ProductID>d0e11</ProductID>
         <ProductRef>OrangeRef</ProductRef>
         <Name>Orange</Name>
         <relatedProducts>
            <RelatedProductID>d0e3</RelatedProductID>
         </relatedProducts>
      </product>
   </products>
</root>

而 libxslt 会产生类似的东西:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <products>
    <product>
      <ProductID>idp116928</ProductID>
      <ProductRef>AppleRef</ProductRef>
      <Name>Apple</Name>
      <relatedProducts>
        <RelatedProductID>idp1506944</RelatedProductID>
      </relatedProducts>
    </product>
    <product>
      <ProductID>idp1506944</ProductID>
      <ProductRef>OrangeRef</ProductRef>
      <Name>Orange</Name>
      <relatedProducts>
        <RelatedProductID>idp116928</RelatedProductID>
      </relatedProducts>
    </product>
  </products>

于 2014-09-12T14:07:09.483 回答