0

我是 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>
4

1 回答 1

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>
于 2013-04-17T17:22:41.107 回答