具有多个名称空间的 XSLT [英] XSLT with multiple namespaces
问题描述
我对样式表很陌生,所以我遇到了一些基本问题.
I am quite new to stylesheets, so I am having some elementary problems.
这是我的 XML:
<NS1:Envelope xmlns:NS1="http://blahblahblah">
<NS1:Body>
<NS2:STR xmlns:NS2="http://blahblah">
<NS2:STD>
<NS2:ST>CA</NS2:ST>
<NS2:CTY>Los Angeles</NS2:CTY>
<NS2:CY>Artesia</NS2:CY>
<NS2:STGC>05</NS2:STGC>
<NS2:CTYGC>037</NS2:CTYGC>
<NS2:CYGC>0160</NS2:CYGC>
<NS2:GC>050370160</NS2:GC>
<NS2:STTR>0.065000</NS2:STTR>
<NS2:CTYTR>0.025000</NS2:CTYTR>
<NS2:CYTR>0.000000</NS2:CYTR>
<NS2:TotalTR>0.090000</NS2:TotalTR>
<NS2:EffectiveDate>2014-09-24</NS2:EffectiveDate>
</NS2:STD>
<NS2:STD>
<NS2:ST>CA</NS2:ST>
<NS2:CTY>Los Angeles</NS2:CTY>
<NS2:CY>Cerritos</NS2:CY>
<NS2:STGC>05</NS2:STGC>
<NS2:CTYGC>037</NS2:CTYGC>
<NS2:CYGC>6430</NS2:CYGC>
<NS2:GC>050370160</NS2:GC>
<NS2:STTR>0.065000</NS2:STTR>
<NS2:CTYTR>0.025000</NS2:CTYTR>
<NS2:CYTR>0.000000</NS2:CYTR>
<NS2:TotalTR>0.090000</NS2:TotalTR>
<NS2:EffectiveDate>2014-09-24</NS2:EffectiveDate>
</NS2:STD>
</NS2:STR>
</NS1:Body>
</NS1:Envelope>
这是我的样式表:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:NS1="http://blahblahblah"
xmlns:NS2="http://blahblah">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/NS1:Envelope/NS1:Body/NS2:STR/NS2:STD">
<xsl:element name="CALL_ENGINE_RESPONSE">
<xsl:value-of select="NS2:ST" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
我得到的回应是:
<?xml version="1.0" encoding="UTF-8"?>
<CALL_ENGINE_RESPONSE>CA</CALL_ENGINE_RESPONSE>
<CALL_ENGINE_RESPONSE>CA</CALL_ENGINE_RESPONSE>
我正在尝试获得类似
<CALL_ENGINE_RESPONSE>
<STATE>CA</STATE>
<STATE>CA</STATE>
</CALL_ENGINE_RESPONSE>
如果我应该编辑我的格式,请告诉我,我很抱歉把它弄这么久.
Please let me know if I should edit my formatting, and I apologize for making it so long.
推荐答案
您对命名空间的处理非常好!
Your handling of namespaces is perfectly fine!
要解决您的问题,您可以做的是拥有一个与 NS2:STD
的共同祖先匹配的模板,并在选择 CALL_ENGINE_RESPONSE
之前在那里输出 CALL_ENGINE_RESPONSE
code>NS2:STD 元素
To solve your problem, what you could do is have a template that matches a common ancestor of your NS2:STD
and output the CALL_ENGINE_RESPONSE
there, before selecting the NS2:STD
element
<xsl:template match="NS2:STR">
<CALL_ENGINE_RESPONSE>
<xsl:apply-templates select="NS2:STD" />
</CALL_ENGINE_RESPONSE>
</xsl:template>
在与 NS2:STD
匹配的模板中,您将输出 STATE
元素
In the template that matches NS2:STD
you would then output the STATE
element
试试这个 XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:NS1="http://blahblahblah" xmlns:NS2="http://blahblah" exclude-result-prefixes="NS1 NS2">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="NS2:STR">
<CALL_ENGINE_RESPONSE>
<xsl:apply-templates select="NS2:STD"/>
</CALL_ENGINE_RESPONSE>
</xsl:template>
<xsl:template match="NS2:STD">
<STATE>
<xsl:value-of select="NS2:ST"/>
</STATE>
</xsl:template>
</xsl:stylesheet>
请注意,无需在模板匹配中放置 NS2:STD
元素的完整路径.仅当在层次结构中的不同位置有其他 NS2:STD
元素您不想匹配时,您才需要这样做.
Note there is no need to put the full path to the NS2:STD
element in the template match. You would only need to do this if there were other NS2:STD
elements at different positions in the hierarchy that you didn't want to match.
这篇关于具有多个名称空间的 XSLT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!