简体   繁体   中英

How can I tell which attributes/elements in my XML file an XSLT will reference?

I have an XML file which is being processed by XSLT, to produce another XML file.

Is there a simple way to know all of the possible elements/attributes in the original XML message that the XSLT will reference in order to produce the output file?

If I look at the example on the w3school.com site ( http://www.w3schools.com/xsl/xsl_transformation.asp ) then the catalogue XML contains items like price and year, but the XSLT will only pull catalog/cd/title and catalog/cd/artist.

So what I need is some kind of automated magic tool that can analyse xslt (maybe the input file schemas) to give me some kind of list of attributes that the output file will contain.

Thanks for any assistance

if you change your xslt file to this

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes"/>

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

it will show you a copy of the original file if that is any help?

good imagination though! Well. Currently there are no such tools available which can analyse XSLT and let you know like 'what all element/attribute it is referring in an XML file'! You have to do it manually ..

It isn't difficult to produce such tool yourself:

Add to any template that matches element(s) or attribute(s) :

<xsl:message>
 <!-- Put all the data here that identifies the element attribute,
      for example the Xpath expression that selects the current node
 -->
</xsl:message>

You can use existing transformations that take a node and produce one XPath expression that selects the node -- invoke them inside the above xsl:message . See for example this: Generate/get xpath from XML node java

Then what you need to do is to combine all these generated XPath expressions with the | operator and to evaluate the resulting XPath expression -- this will select all elements and attributes that are actually referenced in the transformation.

If you also want to account for the elements/attributes that are processed by the XSLT built-in templates , simply override those with specific templates in your transformation -- this is a good practice that as additional benefit may find errors in your transformation.

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