当前位置: 动力学知识库 > 问答 > 编程问答 >

xml - trying to change nodes in xslt following method seen on stackoverflow

问题描述:

I am learning XSLT. I am trying to turn the nodes into nodes following @Rich's advice this post How to replace a node-name with another in Xslt?

Here is my input XML:

<ns:positionSkillResponse xmlns:ns="http://positionskillmanagementservice.webservices.com">

<ns:return>1</ns:return>

<ns:return>9</ns:return>

</ns:positionSkillResponse>

I am trying to transform it with the code below, which is causing a parser error. I think the ns:return is the issue because it the transformer code does not know how to resolve the namespace, ns. But the namespace is not published to a URL, so I can't just point it there. Is that the problem? What do I do?

<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns:

version="1.0">

<xsl:output method="xml" />

<xsl:template match="ns:return">

<parameter>

<xsl:apply-templates select="@*|node()"/>

</parameter>

</xsl:template>

<xsl:template match="@*|node()">

<xsl:copy>

<xsl:apply-templates select="@*|node()"/>

</xsl:copy>

</xsl:template>

Result:

Error:XSLTProcessor::transformToXml() [<a href='xsltprocessor.transformtoxml'>xsltprocessor.transformtoxml</a>]: No stylesheet associated to this object

Testing via this tool:

网友答案:

Your XSL is invalid (it has an unfinished namespace declaration and is missing a closing xsl:stylesheet tag), but even when this is fixed, that tool doesn't seem to work. Try this one instead:

http://www.xslfiddle.net/

When this XSLT is used in that tool with your given input:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns="http://positionskillmanagementservice.webservices.com"
  version="1.0">

  <xsl:output method="xml" />

  <xsl:template match="ns:return">
    <parameter>
      <xsl:apply-templates select="@*|node()"/>
    </parameter>
  </xsl:template>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

The result produced is:

<?xml version="1.0"?><ns:positionSkillResponse xmlns:ns="http://positionskillmanagementservice.webservices.com"><parameter>1</parameter><parameter>9</parameter></ns:positionSkillResponse>
分享给朋友:
您可能感兴趣的文章:
随机阅读: