问题描述:

Hi by using this XSL code

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">

<xsl:output method="xml" indent="yes" version="1.0" omit-xml-declaration="yes"/>

<xsl:template match="/">

<xsl:copy-of select="/"/>

</xsl:template>

</xsl:stylesheet>

I am getting such long XML

<?xml version="1.0" encoding="utf-8"?>

<soa:Label identifier="df0ca28f-1a41-4340-8156-e3ab0c97c559" name="JOB_LOG" description="" instance="464d2ff8-9d2d-404c-965d-907b09e08885" signature="00000000-0000-0000-0000-000000000000" xmlns:soa="urn:telestream.net:soa:core">

<soa:Parameter type="timecode" identifier="d67ee32c-e432-4b5c-853b-739460c82617" bindable="True" name="DLUGOSC" enabled="true" disableable="false">00:00:30:[email protected]<soa:Condition>4949b7da-3253-490d-96b2-d0409ac406ad</soa:Condition>

<soa:Default>00:00:00:[email protected]</soa:Default>

<soa:IsRequired>false</soa:IsRequired>

</soa:Parameter>

<soa:Parameter type="uri" identifier="1c6a9bd8-a2ea-42ad-95e2-9d1ef9c305c0" bindable="True" name="FOLDER ZRODLOWY" editor="filebrowser" enabled="true" disableable="false">G:\PFR-Control\test\KU_DZIEN_ANGLI_ZWIASTUN.mxf<soa:Condition>4dbc7192-aab3-4cb4-b8bd-28ec21f2adb6</soa:Condition>

<soa:Default />

<soa:IsRequired>false</soa:IsRequired>

</soa:Parameter>

<soa:Parameter type="int64" identifier="ada72a89-51f2-400e-af9a-0d68c04f817d" bindable="True" name="WIELKOSC PLIKU" enabled="true" disableable="false">225833056<soa:Condition>fee552e0-ed39-4e15-bc0a-3f52e50f94bc</soa:Condition>

<soa:Default>0</soa:Default>

<soa:IsRequired>false</soa:IsRequired>

</soa:Parameter>

<soa:Parameter type="uri" identifier="123d14bd-3bb1-4a70-b39b-87e14b7c6b5d" bindable="True" name="New Parameter (4)" editor="filebrowser" enabled="true" disableable="false">G:\PFR-Control\test\KU_DZIEN_ANGLI_ZWIASTUN.1.mxf<soa:Condition>e02a7fc1-6a78-4ac4-a7d8-2d4e9807aafe</soa:Condition>

<soa:Default />

<soa:IsRequired>false</soa:IsRequired>

</soa:Parameter>

</soa:Label>

How i can construct XSL file to get just

-00:00:30:[email protected]

-22583305

G:\KU.mov

-G:\KU2.mov

which are parameters I got from aplication

Thanks in advance for answear

网友答案:

There is one thing to note before doing anything else, is that the XML is using a namespace, and so all elements in the XML are part of that namespace. This means you will need to declare the same namespace in your XSLT, so that XSLT can match specific elements within the namespace.

You would declare it as part of the xsl:stylesheet element:

<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:soa="urn:telestream.net:soa:core">

And then you can use the prefix in template matching, to ensure you correctly match the element

 <xsl:template match="soa:Parameter">

Anyway, all the XSLT you are currently using does is copy the XML as-is, without making any changes, so I am guessing you realised the reason the output is so long because it matches the input. So, the first thing to do is throw away the <xsl:template match="/"> template.

Now, XSLT has the concept of 'built-in' templates which will be used when it is looking for templates that match nodes when there is no explicit template in the XSLT file itself. The built-in template will output the text of any text node it finds, otherwise it will skip over the node and continue processing its children.

Although you haven't explained fully, in your question it looks like you want to output the first text node of each soa:Parameter element. The built-in template will do this for you automatically, but it will also output the text of all the child elements of the soa:Parameter element too. Therefore, all you need to do in your XSLT is write a template to ignore the other child elements in this case

<xsl:template match="soa:Parameter/*" />

Of course, if you tried this, you would find it concatenated all the parameter text together in one single string, without spaces, so you can add another template to output the Parameter text with a space in this case.

Try this XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soa="urn:telestream.net:soa:core">
   <xsl:output method="xml" indent="yes"/>

   <xsl:template match="soa:Parameter">
       <xsl:value-of select="concat(text(), ' ')" />
   </xsl:template>

   <xsl:template match="soa:Parameter/*" />
</xsl:stylesheet>
相关阅读:
Top