编辑:示例现在包含我的主文档中的所有标记
大家好!我刚才有一个关于XSLT的快速问题。我有一个大的xml文件,其中有许多<DIMENSION_Id>
个节点互相嵌套。在每个<DIMENSION_Id>
节点中有两个SYN标记:<SYN>String</SYN><SYN>Integer</SYN>
我要做的是获取每个DIMENSION_Id的最远子节点并将其与其所有祖先路径连接以创建URL。
即
<DIMENSIONS VERSION="1.0.0">
<DIMENSION NAME="Category" SRC_TYPE="INTERNAL">
<DIMENSION_NODE ID="1000"/>
<DIMENSION_Id>
<SYN>Text</SYN>
<SYN>Number</SYN>
<DIMENSION_Id>
<SYN>More Text</SYN>
<SYN>Another Number</SYN>
</DIMENSION_Id>
</DIMENSION_Id>
</DIMENSION>
</DIMENSIONS>
我编写此XSLT以首先从父节点获取所有信息,然后子节点最后创建一个完整的URL。不幸的是,它只给了我最远的子节点的信息...我不知道如何附加任何其他文本。 (它应该读取类似:最远 - 父/更近 - 父/父/ item_selected)
不幸的是它所做的只是给我当前节点的值....这是我写的XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="/DIMENSION_NODE">
<xsl:for-each select="ancestor-or-self::*">
<xsl:value-of select="@SYN" />
<xsl:text>/</xsl:text>
<xsl:value-of select="." />
<xsl:value-of select="@SYN" />
<xsl:text>/</xsl:text>
<xsl:value-of select="." />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
提前感谢您的帮助!
答案 0 :(得分:1)
编辑:输入更接近问题的样本。
使用此输入:
<DIMENSIONS VERSION="1.0.0">
<DIMENSION NAME="Category" SRC_TYPE="INTERNAL">
<DIMENSION_NODE ID="1000"/>
<DIMENSION_Id>
<SYN>Text</SYN>
<SYN>1</SYN>
<DIMENSION_Id>
<SYN>More Text</SYN>
<SYN>2</SYN>
</DIMENSION_Id>
</DIMENSION_Id>
</DIMENSION>
</DIMENSIONS>
两个选项。
1)使用模式将模板应用于祖先:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="text()"/>
<xsl:template match="SYN[number()!=.]">
<xsl:apply-templates select="ancestor::DIMENSION_Id" mode="output"/>
<xsl:value-of select="concat(' ',../SYN[number()=.],'
')"/>
</xsl:template>
<xsl:template match="DIMENSION_Id" mode="output">
<xsl:value-of select="concat('/',SYN[number()!=.])"/>
</xsl:template>
</xsl:stylesheet>
2)使用params:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="text()"/>
<xsl:template match="SYN[number()!=.]">
<xsl:param name="pPath"/>
<xsl:value-of select="concat($pPath,' ',../SYN[number()=.],'
')"/>
</xsl:template>
<xsl:template match="DIMENSION_Id">
<xsl:param name="pPath"/>
<xsl:apply-templates>
<xsl:with-param name="pPath"
select="concat($pPath,'/',SYN[number()!=.])"/>
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>
两个输出:
/Text 1
/Text/More Text 2
答案 1 :(得分:1)
我想你想要这个:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="DIMENSION_Id[not(DIMENSION_Id)]">
<xsl:apply-templates select="(.|ancestor::DIMENSION_Id)/SYN" mode="gen"/>
</xsl:template>
<xsl:template match="SYN" mode="gen">
<xsl:value-of select="concat('/',.)"/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
将此转换应用于提供的XML文档(已更正为格式正确):
<DIMENSIONS VERSION="1.0.0">
<DIMENSION NAME="Category" SRC_TYPE="INTERNAL">
<DIMENSION_NODE ID="1000"/>
<DIMENSION_Id>
<SYN>Text</SYN>
<SYN>Number</SYN>
<DIMENSION_Id>
<SYN>More Text</SYN>
<SYN>Another Number</SYN>
</DIMENSION_Id>
</DIMENSION_Id>
</DIMENSION>
</DIMENSIONS>
产生了想要的正确结果:
/Text/Number/More Text/Another Number
答案 2 :(得分:0)
对不起大家的困惑。我收到了一些帮助,这是解决方案:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="/">
<URLs>
<xsl:apply-templates select="//DIMENSION_NODE"/>
</URLs>
</xsl:template>
<xsl:template match="DIMENSION_NODE">
<xsl:call-template name="getPath">
<xsl:with-param name="currentnode" select="."/>
</xsl:call-template>
</xsl:template>
<xsl:template name="getPath">
<xsl:param name="currentnode"/>
<xsl:param name="currenttext" select="''"/>
<xsl:param name="firstrun" select="1"/>
<xsl:choose>
<xsl:when test="$currentnode[parent::DIMENSION]">
<URL>
<xsl:value-of select="$currenttext"/>
</URL>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="$firstrun = 1">
<xsl:variable name="gettext">
<xsl:text>/</xsl:text>
<xsl:value-of select="concat($currentnode/DVAL/SYN[1],'	',$currentnode/DVAL/DVAL_ID/@ID)"/>
</xsl:variable>
<xsl:call-template name="getPath">
<xsl:with-param name="currentnode" select="$currentnode/.."/>
<xsl:with-param name="currenttext" select="concat($gettext,$currenttext)"/>
<xsl:with-param name="firstrun" select="0"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="gettext">
<xsl:text>/</xsl:text>
<xsl:value-of select="$currentnode/DVAL/SYN[1]"/>
</xsl:variable>
<xsl:call-template name="getPath">
<xsl:with-param name="currentnode" select="$currentnode/.."/>
<xsl:with-param name="currenttext" select="concat($gettext,$currenttext)"/>
<xsl:with-param name="firstrun" select="0"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
我也为XML文件中的错误道歉。