简体   繁体   中英

Fetch an XML node-name as regex in XSL Stylesheet transformation

I have an XML (I know it is incorrect as per XML standards but I am restricted to changes as I am processing response from external party) as follows,

 XML Snippet : <root>
                 <3party>some_value</3party>
               </root>

I would like to fetch <3party> from above snippet in XSL stylesheet transformation. The fact is the <3party> element is invalid so I can no refer it as follows as it fails the xsl compilation. I would need a way to refer it as a partial element may be using some regx way? Following is incorrect.

   <xsl:variable select="$response/root/3party" />

Any answers would help me out.

Edit : Possible solution to above usecase would be.

<xsl:for-each select="$response/root">
    <!-- Node name -->
         <xsl:variable name="name" select="local-name()" />
     <!-- check if it contains party -->
         <xsl:if test="contains($name, 'party')">
             <!-- Node value -->
             <xsl:variable name="value" select="node()" />
         </xsl:if>
</xsl:for-each>

With regard to the code added to your question:

<xsl:for-each select="$response/root">
    <!-- Node name -->
         <xsl:variable name="name" select="local-name()" />
     <!-- check if it contains party -->
         <xsl:if test="contains($name, 'party')">
             <!-- Node value -->
             <xsl:variable name="value" select="node()" />
         </xsl:if>
</xsl:for-each>

The test contains($name, 'party') will never return true. The context node's name is root and "root" does not contain "party".


On a more general note: you seem to think that the problem is how to get your XSLT stylesheet to compile. That is not so. The real problem here is that your input is not well-formed XML. The input must be parsed before it can be transformed, and if it's not well-formed XML, the process will fail well before even considering your stylesheet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM