将XML文档转换为URL时出现问题
编辑:该示例现在包含我的主文档中的所有标记 大家好!我刚才有一个关于XSLT的简短问题。我有一个很大的xml文件,其中有许多嵌套在彼此内部的将XML文档转换为URL时出现问题,xml,xslt,Xml,Xslt,编辑:该示例现在包含我的主文档中的所有标记 大家好!我刚才有一个关于XSLT的简短问题。我有一个很大的xml文件,其中有许多嵌套在彼此内部的节点。在每个节点中有两个SYN标记:StringInteger我试图做的是获取每个维度Id的最远子节点,并将其与其所有祖先路径连接以创建URL i、 e 正文 数 更多文本 另一个号码 我编写此XSLT是为了首先从父节点获取所有信息,然后从子节点获取所有信息,最后创建完整的URL。不幸的是,它只给我最远的子节点的信息…我不知道如何向它附加任何其他文本。(
节点。在每个
节点中有两个SYN标记:StringInteger
我试图做的是获取每个维度Id的最远子节点,并将其与其所有祖先路径连接以创建URL
i、 e
正文
数
更多文本
另一个号码
我编写此XSLT是为了首先从父节点获取所有信息,然后从子节点获取所有信息,最后创建完整的URL。不幸的是,它只给我最远的子节点的信息…我不知道如何向它附加任何其他文本。(应该是这样的:最远的父项/较近的父项/父项/所选项目)
不幸的是,它所做的只是给我当前节点的值。。。。以下是我编写的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>
/
/
提前感谢您的帮助 编辑:输入更接近问题的样本 通过此输入:
<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>
我想你想要这个:
<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 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文件中的错误道歉。你的问题表达得很糟糕。您的输入示例与样式表或所需结果不匹配。请更正。请原谅我,但我不明白你需要从我这里知道什么信息。我想要的是从父SYN到子SYN中获取值,使其看起来像URL,即:每个XML节点的祖父母SYN值/父SYN值/当前节点SYN值。接下来我需要做的是将第二个SYN值中的整数与URL匹配…但我主要关心的是实际的URL字符串。如果您需要我进一步澄清,请让我知道..我所说的措词不当的问题是:
祖父母/父母/父母。。。但是您有两个SYN
元素;您的样式表与输入样本中不存在的DIMENSION\u节点
根元素匹配;您正试图输出带有
的SYN
属性和维度节点的字符串值
元素…好问题(+1)。请参阅我的答案,以获得完整且非常简短的解决方案。:)谢谢大家!我明白你的意思,亚历杭德罗。文档是Text Number More Text另一个数字,基本上我现在忽略了这些数字,但我想在完成的url字符串末尾获取数字。i、 e.祖父母文本/父母文本/当前文本/当前文本/当前文本/数字..对于第一个,我使用SYN切换文件,使用维度Id切换名称..但不起作用。我不能真正理解你的第二个例子,我很抱歉…我对XSLT非常陌生。嗯…这次我没有得到任何输出…但我认为很多是因为我自己没有正确地发布文档。我现在已经把它编辑成正确的了。很抱歉造成混淆。@Daniel:对于您的新XML文档,我得到了完全相同的结果。您是否更正了它,因为您所呈现的格式不正确,并且会导致解析器错误。我很少对其他问题或答案投反对票,但这是错误的。此外,它冗长且设计错误(不是XSLT样式,即$firstrun
或调用命名模板将上下文节点作为参数传递),它对任何人都没有帮助,因为它与您的输入样本不匹配(DVAL
和DVAL\u ID
元素)。
<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
<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>
<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
<?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>