开发者

XSL help creating comma separated XML

开发者 https://www.devze.com 2022-12-20 22:27 出处:网络
I\'m stuck with something which is probably quite simple to resolve but don\'t have a clue. My XML data is as follows :-

I'm stuck with something which is probably quite simple to resolve but don't have a clue.

My XML data is as follows :-

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="GetTicketCategories.xsl"?>
<ArrayOfCategory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://test.com/test/">
  <Category>
    <Name>Benefits</Name>
    <Count>29</Count>
  </Category>
  <Category>
    <Name>Building Control</Name>
    <Count>4</Count>
  </Category>
 </ArrayOfCategory>

XSL file :-

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text"/> 
<xsl:template match="Category">

<xsl:value-of select="Name"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="Count"/>
<xsl:text>, </xsl:text>

</xsl:template>
</xsl:stylesheet>

The output is :-

Benefits29Building Control4

There seems to be an issue with the top element where it doesn't like its formatting e.g. <ArrayOfCategory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://ctronix.com/smartticketservice/">

I'm not in a position to change the xml but if I simplify the code it works as below :-

Changed XML :-

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="GetTicketCategories.xsl"?>
<ArrayOfCategory>
  <Category>
    <Name>Benefits</Name>
    <Count>29</Count>
  </Category>
  <Category>
    <Name>Building Control</Name>
    <Count>4</Count>
  </Category>
 </ArrayOfCategory>

XSL:-

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text"/> 
<xsl:template match="ArrayOfCategory/Category">

<xsl:value-of select="Name"/>
<xsl:text>, </xsl:text&开发者_开发知识库gt;
<xsl:value-of select="Count"/>
<xsl:text>, </xsl:text>

</xsl:template>
</xsl:stylesheet>

Correct Output:-

Benefits, 29, Building Control, 4,

So how do I get the required output using the existing XML file? I don't know how to use

<ArrayOfCategory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://ctronix.com/smartticketservice/">

in the template match section.

I must confess I'm a complete beginner; any help would be very much appreciated.

Cheers

Rich


It appears that the first XSL template you posted does not match anything, so it defaults to outputting all text elements.

You need to change the template match - use /ArrayOfCategory/Category, which translates to "from the root of the xml, select all ArrayOfCategory/Category":

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/> 

 <xsl:template match="/ArrayOfCategory/Category">

  <xsl:value-of select="Name"/>
  <xsl:text>, </xsl:text>
  <xsl:value-of select="Count"/>
  <xsl:text>, </xsl:text>

 </xsl:template>
</xsl:stylesheet>


You have to reference in XSL namespace used in XML. Check this XSL:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:t="http://test.com/test/">
  <xsl:output method="text"/>
  <xsl:template match="t:ArrayOfCategory/t:Category">
    <xsl:value-of select="t:Name"/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="t:Count"/>
    <xsl:text>,</xsl:text>
  </xsl:template>
</xsl:stylesheet>

With this XML:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="GetTicketCategories.xsl"?>
<ArrayOfCategory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://test.com/test/">
  <Category>
    <Name>Benefits</Name>
    <Count>29</Count>
  </Category>
  <Category>
    <Name>Building Control</Name>
    <Count>4</Count>
  </Category>
</ArrayOfCategory>

Result:

Benefits,29,Building Control,4,
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号