我是XSLT的新手,我很难弄清楚如何翻译以下内容:
<resource>
<id>12345678</id>
<attribute name="partID">12345678-222</attribute>
</resource>
为:
<Attributes Category="urn:oasis:names:tc:xacml:3.0:attribute-category:resource">
<Attribute IncludeInResult="false" AttributeId="urn:oasis:names:tc:xacml:1.0:resource:resource-id">
<AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">12345678</AttributeValue>
</Attribute>
<Attribute IncludeInResult="false" AttributeId="partID">
<AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">12345678</AttributeValue>
</Attribute>
</Attributes>
答案 0 :(得分:1)
像这样的简单样式表可以解决这个问题:
<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"/>
<!-- Match <resource> elements -->
<xsl:template match="resource">
<!-- Create a new literal <Attributes> element -->
<Attributes
Category="urn:oasis:names:tc:xacml:3.0:attribute-category:resource">
<!-- Apply the child elements of this element -->
<xsl:apply-templates/>
</Attributes>
</xsl:template>
<xsl:template match="id">
<Attribute IncludeInResult="false"
AttributeId="urn:oasis:names:tc:xacml:1.0:resource:resource-id">
<AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">
<!--
Add the value of the text node of the original <id> element.
That is, "12345678".
-->
<xsl:value-of select="."/>
</AttributeValue>
</Attribute>
</xsl:template>
<xsl:template match="attribute">
<!--
Use Attribute Value Templates to use the value of the @name attribute
as the value of the @AttributeId attribute.
See http://lenzconsulting.com/how-xslt-works/#attribute_value_templates
-->
<Attribute IncludeInResult="false" AttributeId="{@name}">
<AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">
<!--
Use the substring before the hyphen as the value of this
<AttributeValue> element (again, "12345678", omitting the
"-222" part)
-->
<xsl:value-of select="substring-before(., '-')"/>
</AttributeValue>
</Attribute>
</xsl:template>
</xsl:stylesheet>