XSLT

时间:2016-01-05 10:37:17

标签: xslt xslt-1.0 xslt-2.0 xslt-grouping

由于attrQualMany在两个地方出现并且每个都有2个值,我有一个循环4次的XML。 所以2 * 2

示例XML消息是

<document>
<ObjectId>
    <ID>1000909090</ID>
    <dlex>
        <attrGroupMany name="streetinfo">
            <row>                                             
                <attr name="streetcode">AS_DRAINED</attr> 
                <attrQualMany name="streetintake">          
                    <value qual="en">dvif1</value>
                    <value qual="nl">dvif2</value>
                </attrQualMany>
                <attr name="streettype">BY_MEASURE</attr>
                <attrQual name="streetbasis" qual="ONZ">5</attrQual>
                <attrQual name="streetsize" qual="EA">1</attrQual>
                <attrQualMany name="streetsizeDescription">  
                    <value qual="en">sz1</value>
                    <value qual="hi">sz2</value>
                </attrQualMany>
            </row>
        </attrGroupMany>
    </dlex>
</ObjectId>
</document>

我正在使用以下XSLT

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

<xsl:output method="xml" indent="yes"/>

<xsl:template match="/document">
    <CatalogObjectId>
        <RelationshipData>
            <Relationship>
                <RelationType>ObjectId_Street</RelationType>
                <RelatedObjectIds>
                    <xsl:for-each select="ObjectId/dlex/attrGroupMany[@name='streetinfo']">
                        <xsl:variable name="pos" select="position()"/>
                        <xsl:apply-templates select="row" mode="streetintake">
                            <xsl:with-param name="referenceKey" select="concat('ObjectId_Street-',$pos)" />
                        </xsl:apply-templates>
                    </xsl:for-each>
                </RelatedObjectIds>
            </Relationship>
        </RelationshipData>
    </CatalogObjectId>
</xsl:template>

<xsl:template match="row" mode="streetintake">
    <xsl:param name="referenceKey" />
    <xsl:variable name="streetcode" select="concat($referenceKey,'-',attr[@name='streetcode'],'-')"/>
    <xsl:choose>
        <xsl:when test="attrQualMany[@name='streetintake']/value">
            <xsl:for-each select="attrQualMany[@name='streetintake']/value">
                <xsl:apply-templates select="../.." mode="streetsize">
                    <xsl:with-param name="referenceKey" select="concat($streetcode,text(),'-',@qual)" />
                </xsl:apply-templates>
            </xsl:for-each>
        </xsl:when>
        <xsl:otherwise>
            <xsl:apply-templates select="." mode="streetsize">
                <xsl:with-param name="referenceKey" select="$streetcode" />
            </xsl:apply-templates>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template match="row" mode="streetsize">
    <xsl:param name="referenceKey" />
    <xsl:choose>
        <xsl:when test="attrQualMany[@name='streetsizeDescription']/value">
            <xsl:for-each select="attrQualMany[@name='streetsizeDescription']/value">
                <xsl:call-template name="RelatedObjectId">
                    <xsl:with-param name="referenceKey" select="concat($referenceKey,'-',text(),'-',@qual)" />
                </xsl:call-template>
            </xsl:for-each>
        </xsl:when>
        <xsl:otherwise>
            <xsl:call-template name="RelatedObjectId">
                <xsl:with-param name="referenceKey" select="$referenceKey" />
            </xsl:call-template>                
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template name="RelatedObjectId">
    <xsl:param name="referenceKey" />
    <RelatedObjectId>
        <xsl:attribute name="referenceKey">
            <xsl:variable name="v_pos" select="position()"/>
            <xsl:value-of select="concat($referenceKey,'-',$v_pos)" />          
        </xsl:attribute>
    </RelatedObjectId>        
</xsl:template>

</xsl:stylesheet>

我得到的当前输出是

<?xml version="1.0" encoding="UTF-8"?>
<CatalogObjectId>
<RelationshipData>
  <Relationship>
     <RelationType>ObjectId_Street</RelationType>
     <RelatedObjectIds>
        <RelatedObjectId referenceKey="ObjectId_Street-1-AS_DRAINED-dvif1-en-sz1-en-1" />
        <RelatedObjectId referenceKey="ObjectId_Street-1-AS_DRAINED-dvif1-en-sz2-hi-2" />
        <RelatedObjectId referenceKey="ObjectId_Street-1-AS_DRAINED-dvif2-nl-sz1-en-1" />
        <RelatedObjectId referenceKey="ObjectId_Street-1-AS_DRAINED-dvif2-nl-sz2-hi-2" />
     </RelatedObjectIds>
  </Relationship>
</RelationshipData>
</CatalogObjectId>

但是我希望每个循环都有增量,预期的输出应该是

<?xml version="1.0" encoding="UTF-8"?>
<CatalogObjectId>
<RelationshipData>
  <Relationship>
     <RelationType>ObjectId_Street</RelationType>
     <RelatedObjectIds>
        <RelatedObjectId referenceKey="ObjectId_Street-1-AS_DRAINED-dvif1-en-sz1-en-1" />
        <RelatedObjectId referenceKey="ObjectId_Street-1-AS_DRAINED-dvif1-en-sz2-hi-2" />
        <RelatedObjectId referenceKey="ObjectId_Street-1-AS_DRAINED-dvif2-nl-sz1-en-3" />
        <RelatedObjectId referenceKey="ObjectId_Street-1-AS_DRAINED-dvif2-nl-sz2-hi-4" />
     </RelatedObjectIds>
  </Relationship>
</RelationshipData>
</CatalogObjectId>

请你把我放在哪里放置位置计数器,我尝试了很多组合,但没有运气

1 个答案:

答案 0 :(得分:0)

让我们将示例缩减到演示问题所需的最小值。鉴于以下输入:

<强> XML

<row> 
    <attr name="A">          
        <value>A</value>
        <value>B</value>
        <value>C</value>
        <value>D</value>
    </attr>
    <attr name="B">          
        <value>X</value>
        <value>Y</value>
        <value>Z</value>
    </attr>
</row>

以下样式表:

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:template match="row">
    <objects>
        <xsl:for-each select="attr[@name='A']/value">
            <xsl:variable name="i" select="position()"/>
            <xsl:for-each select="../../attr[@name='B']/value">         
                <object>
                    <xsl:value-of select="($i - 1) * last() + position()" />
                </object>
            </xsl:for-each>     
        </xsl:for-each>
    </objects>
</xsl:template>

</xsl:stylesheet>

将返回:

<强>结果

<?xml version="1.0" encoding="UTF-8"?>
<objects>
   <object>1</object>
   <object>2</object>
   <object>3</object>
   <object>4</object>
   <object>5</object>
   <object>6</object>
   <object>7</object>
   <object>8</object>
   <object>9</object>
   <object>10</object>
   <object>11</object>
   <object>12</object>
</objects>