Select 个节点下面的节点并将属性值转换为元素
Select nodes below certain node and convert attribute values into elements
我有以下 XML:
<A someattributes>
<B someotherattributes>
<Value XX="Attr1">SomeInfo1</Value>
<Value XX="Attr2"></Value>
<Value XX="Attr3">SomeInfo3</Value>
<Value XX="Attr4">SomeInfo4</Value>
</B>
</A>
我的 XSLT 应生成以下输出:
<IMPORT>
<Attr1>SomeInfo1</Attr1>
<Attr3>SomeInfo3</Attr3>
<Attr4>SomeInfo4</Attr4>
</IMPORT>
那么,我的要求是
- 顶级节点应为“IMPORT”(应删除“值”以上的所有节点)
- 属性“XX”的所有属性值都应转换为元素
- 如果一个元素没有值(如上例中的XX="Attr2"),则不应添加。
对于属性和元素之间的转换,我尝试遵循这个线程:
Convert attribute value into element
但不幸的是我无法调整它以获得我上面描述的输出(导入节点,排除空元素)
这是我所拥有的,但它没有产生正确的输出:
<?xml version="1.0" encoding="UTF-8"?>
<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="@* | node()">
<!--<xsl:template match="/">-->
<IMPORT>
<xsl:for-each select="Value">
<xsl:call-template name="iter">
</xsl:call-template>
</xsl:for-each>
</IMPORT>
</xsl:template>
<xsl:template name="iter">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="Value">
<xsl:element name="{@XX}">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
你能不能做一些简单的事情,比如:
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="/A">
<IMPORT>
<xsl:for-each select="B/Value[@XX][text()]">
<xsl:element name="{@XX}">
<xsl:value-of select="." />
</xsl:element>
</xsl:for-each>
</IMPORT>
</xsl:template>
</xsl:stylesheet>
我有以下 XML:
<A someattributes>
<B someotherattributes>
<Value XX="Attr1">SomeInfo1</Value>
<Value XX="Attr2"></Value>
<Value XX="Attr3">SomeInfo3</Value>
<Value XX="Attr4">SomeInfo4</Value>
</B>
</A>
我的 XSLT 应生成以下输出:
<IMPORT>
<Attr1>SomeInfo1</Attr1>
<Attr3>SomeInfo3</Attr3>
<Attr4>SomeInfo4</Attr4>
</IMPORT>
那么,我的要求是
- 顶级节点应为“IMPORT”(应删除“值”以上的所有节点)
- 属性“XX”的所有属性值都应转换为元素
- 如果一个元素没有值(如上例中的XX="Attr2"),则不应添加。
对于属性和元素之间的转换,我尝试遵循这个线程: Convert attribute value into element
但不幸的是我无法调整它以获得我上面描述的输出(导入节点,排除空元素)
这是我所拥有的,但它没有产生正确的输出:
<?xml version="1.0" encoding="UTF-8"?>
<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="@* | node()">
<!--<xsl:template match="/">-->
<IMPORT>
<xsl:for-each select="Value">
<xsl:call-template name="iter">
</xsl:call-template>
</xsl:for-each>
</IMPORT>
</xsl:template>
<xsl:template name="iter">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="Value">
<xsl:element name="{@XX}">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
你能不能做一些简单的事情,比如:
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="/A">
<IMPORT>
<xsl:for-each select="B/Value[@XX][text()]">
<xsl:element name="{@XX}">
<xsl:value-of select="." />
</xsl:element>
</xsl:for-each>
</IMPORT>
</xsl:template>
</xsl:stylesheet>