Xml 需要从XSL中获取属性值
我一直致力于生成元素和属性名称以及输出元素和属性。在这里,我使用OxyXMLEditor 18.0将输入xml转换为DocBookXML 我的输入xml如下所示:Xml 需要从XSL中获取属性值,xml,xslt,attributes,xslt-2.0,Xml,Xslt,Attributes,Xslt 2.0,我一直致力于生成元素和属性名称以及输出元素和属性。在这里,我使用OxyXMLEditor 18.0将输入xml转换为DocBookXML 我的输入xml如下所示: <Section1 id="1"> <Section1Heading>Section 1</Section1Heading> <Section2 id="2"> <Section2Heading>Heading</Section2Heading>
<Section1 id="1">
<Section1Heading>Section 1</Section1Heading>
<Section2 id="2">
<Section2Heading>Heading</Section2Heading>
<Para>other.</Para>
</Section2>
</Section1>
我认为这将产生您需要的输出,尽管它依赖于具有非常有限结构的输入XSLT(例如,它假定
xsl:attribute
始终包含xsl:value
)
使用match=“Section1”
我不知道Section1/@id
会选择什么,没有嵌套的Section1
元素。我现在已经编辑了。请see@MartinHonnen预期输出元素和属性的顺序也不是问题。如果它重新排序也没有问题,我会得到像标签、@id
这样的属性。但我需要它作为@id,label
。因为输入是@id,输出是label,我修改了我的答案,在相关的concat
语句中交换两个参数,所以它们现在应该是正确的顺序。
<xsl:template match="Section1">
<sect1>
<xsl:attribute name="label">
<xsl:value-of select="@id"/>
</xsl:attribute>
<xsl:apply-templates/>
</sect1>
</xsl:template>
<xsl:template match="Section1Heading | Section2Heading">
<title>
<xsl:apply-templates/>
</title>
</xsl:template>
<xsl:template match="Section2">
<sect2>
<xsl:attribute name="label">
<xsl:value-of select="@id"/>
</xsl:attribute>
<xsl:apply-templates/>
</sect2>
</xsl:template>
<xsl:template match="Para">
<para>
<xsl:apply-templates/>
</para>
</xsl:template>
<xsl:param name="field-sep" as="xs:string">,</xsl:param>
<xsl:param name="line-sep" as="xs:string" select="' '"/>
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:value-of select="concat('Input XML', $field-sep, 'Result XML')"/>
<xsl:value-of select="$line-sep"/>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="xsl:template[@match]">
<xsl:value-of
select="for $pattern in tokenize(@match, '\s*\|\s*')
return concat($pattern, $field-sep, node-name(current()/*[1]))"
separator="{$line-sep}"/>
<xsl:value-of select="$line-sep"/>
</xsl:template>
Input XML,Result XML
Section1,sect1
@id,label
Section1Heading,title
Section2Heading,title
Section2,sect2
@id,label
Para,para
<xsl:template match="xsl:template[@match]">
<xsl:variable name="current" select="." />
<xsl:for-each select="tokenize(@match, '\s*\|\s*')">
<xsl:value-of select="concat(., $field-sep, node-name($current/*[1]))" />
<xsl:value-of select="$line-sep"/>
<xsl:for-each select="$current/*[1]/xsl:attribute">
<xsl:value-of select="concat(xsl:value-of[1]/@select, $field-sep, @name)" />
<xsl:value-of select="$line-sep"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>