다음 XML이 있습니다.
<?xml version="1.0" encoding="UTF-8"?>
<typeNames xmlns="http://www.dsttechnologies.com/awd/rest/v1" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<typeName recordType="case" href="awdServer/awd/services/v1/businessareas/SAMPLEBA/types/SAMPLECASE">SAMPLECASE</typeName>
<typeName recordType="folder" href="awdServer/awd/services/v1/businessareas/SAMPLEBA/types/SAMPLEFLD">SAMPLEFLD</typeName>
<typeName recordType="source" href="awdServer/awd/services/v1/businessareas/SAMPLEBA/types/SAMPLEST">SAMPLEST</typeName>
<typeName recordType="transaction" href="awdServer/awd/services/v1/businessareas/SAMPLEBA/types/SAMPLEWT">SAMPLEWT</typeName>
</typeNames>
XSLT를 사용하여 아래와 같이 xml 위의 변환을 원합니다.
<response>
<results>
<source>
SAMPLEST
</source>
</results>
</response>
</xsl:template>
입력 xml에서 출력 xml로 소스를 가져오고 싶습니다.
다음 xml로 시도하고 있지만 필요한 출력 xml을 가져올 수 없습니다.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:v="http://www.dsttechnologies.com/awd/rest/v1" version="2.0" exclude-result-prefixes="v">
<xsl:output method="xml" version="1.0" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*" />
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="typeNames">
<response>
<results>
<source>
<xsl:value-of select="source" />
</source>
</results>
</response>
</xsl:template>
</xsl:stylesheet>
I. 입력 XML의 네임 스페이스
<typeNames xmlns="http://www.dsttechnologies.com/awd/rest/v1"...
xmlns
self + 모든 자식 노드를 네임 스페이스에 넣습니다. 이 네임 스페이스에는 접두사가 필요하지 않습니다.
II. XSLT의 네임 스페이스
... xmlns:v="http://www.dsttechnologies.com/awd/rest/v1"...
네임 스페이스 (소스와 동일한 URI) 앞에를 붙 v
였으므로 xpath에도이 접두사를 작성해야합니다.
<xsl:template match="v:typeNames">
[XSLT 2.0 : xpath-default-namespace="uri"
스타일 시트 섹션에 추가 하여 모든 xpath-expression에 대한 기본 네임 스페이스를 정의 할 수도 있습니다 . 따라서 네임 스페이스를 접두사로 지정할 필요가 없습니다.]
III. 주어진 입력 xml 추측
<xsl:value-of select="source" /> -> <typeName recordType="source"..>SAMPLEST</typeName>
표시된 xml-node를 선택하려면 다음 중 하나를 작성해야합니다.
absolute, without any context node:
/v:typeNames/v:typeName[@recordType = 'source']
on context-node typeNames:
v:typeName[@recordType = 'source']
[ <xsl:value-of select="..."/>
텍스트 노드를 반환합니다. 예 : "SAMPLEST"]
편집하다:
태그가 두 개 있으면 어떨까요?
가장 먼저해야 할 일 : <xsl:value-of
XSLT 1에서는 1 개의 노드에서만 작동 할 수 있습니다! xpath 표현식이 둘 이상의 노드와 일치하면 첫 번째 노드 만 처리합니다!
다음과 같이 해결하십시오.
...
<results>
<xsl:apply-templates select="v:typeName[@recordType = 'source']"/>
</results>
...
<xsl:template match="v:typeName[@recordType = 'source']">
<source>
<xsl:value-of select="."/>
</source>
</xsl:template>
apply-templates
내 results
모든 검색 typeName..source
. 일치하는 템플릿은 해당 노드를 수신하고 xml을 만듭니다 <source>...
.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다